package com.qixin.modules.rzInvoice.service.impl;

import com.qixin.modules.rzInvoice.entity.EaRzZzsdzptfp;
import com.qixin.modules.rzInvoice.entity.EaRzZzsdzptfpItems;
import com.qixin.modules.rzInvoice.mapper.EaRzZzsdzptfpItemsMapper;
import com.qixin.modules.rzInvoice.mapper.EaRzZzsdzptfpMapper;
import com.qixin.modules.rzInvoice.service.IEaRzZzsdzptfpService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 增值税电子普通发票
 * @Author: qixin-boot
 * @Date:   2021-07-10
 * @Version: V1.0
 */
@Service
public class EaRzZzsdzptfpServiceImpl extends ServiceImpl<EaRzZzsdzptfpMapper, EaRzZzsdzptfp> implements IEaRzZzsdzptfpService {

	@Autowired
	private EaRzZzsdzptfpMapper eaRzZzsdzptfpMapper;
	@Autowired
	private EaRzZzsdzptfpItemsMapper eaRzZzsdzptfpItemsMapper;
	
	@Override
	@Transactional
	public void saveMain(EaRzZzsdzptfp eaRzZzsdzptfp, List<EaRzZzsdzptfpItems> eaRzZzsdzptfpItemsList) {
		eaRzZzsdzptfpMapper.insert(eaRzZzsdzptfp);
		if(eaRzZzsdzptfpItemsList!=null && eaRzZzsdzptfpItemsList.size()>0) {
			for(EaRzZzsdzptfpItems entity:eaRzZzsdzptfpItemsList) {
				//外键设置
				entity.setRzZzsdzptfpId(eaRzZzsdzptfp.getId());
				eaRzZzsdzptfpItemsMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void updateMain(EaRzZzsdzptfp eaRzZzsdzptfp,List<EaRzZzsdzptfpItems> eaRzZzsdzptfpItemsList) {
		eaRzZzsdzptfpMapper.updateById(eaRzZzsdzptfp);
		
		//1.先删除子表数据
		eaRzZzsdzptfpItemsMapper.deleteByMainId(eaRzZzsdzptfp.getId());
		
		//2.子表数据重新插入
		if(eaRzZzsdzptfpItemsList!=null && eaRzZzsdzptfpItemsList.size()>0) {
			for(EaRzZzsdzptfpItems entity:eaRzZzsdzptfpItemsList) {
				//外键设置
				entity.setRzZzsdzptfpId(eaRzZzsdzptfp.getId());
				eaRzZzsdzptfpItemsMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void delMain(String id) {
		eaRzZzsdzptfpItemsMapper.deleteByMainId(id);
		eaRzZzsdzptfpMapper.deleteById(id);
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			eaRzZzsdzptfpItemsMapper.deleteByMainId(id.toString());
			eaRzZzsdzptfpMapper.deleteById(id);
		}
	}
	
}
